package com.gears_software.sxdc.dccx.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IGradeFinder;
import com.gears_software.sxdc.dccx.data.finder.RequestUrl;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.CPOrderDetailModel;
import com.gears_software.sxdc.dccx.data.model.MyTripPassengerDetailModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.request.RComplainModel;
import com.gears_software.sxdc.dccx.data.model.request.RGradeModel;
import com.sdsmdg.tastytoast.TastyToast;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.text.SimpleDateFormat;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/7/31.
 */
@EActivity(R.layout.have_completed)
public class HaveCompletedActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView tv_call_phone;
    @ViewById
    RatingBar rb_me_ratingBar;
    @ViewById
    Button btn_submit;
    @ViewById
    SimpleDraweeView img_avatar;
    @ViewById
    TextView txt_nickname;
    @ViewById
    TextView tv_phone_number;
    @ViewById
    TextView tv_chepaihao;
    @ViewById
    TextView tv_chexing;
    @ViewById
    TextView tv_color;
    @ViewById
    TextView tv_money;
    @ViewById
    RelativeLayout re_carinfo;
    @Extra
    String flag;
    @Extra
    String id;
    @Extra
    CPOrderDetailModel orderDetailModel;
    @Extra
    MyTripPassengerDetailModel detailModel;
    @RestService
    IGradeFinder gradeFinder;
    @Bean
    GearsErrorHandler errorHandler;
    Context context;

    @AfterViews
    void created() {
        context = this;
        actionbar_title.setText("已完成");
        gradeFinder.setRestErrorHandler(errorHandler);
        initIconfont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        if (flag != null && !flag.equals("")) {
            if (flag.equals("1")) {
                re_carinfo.setVisibility(View.GONE);
                if (orderDetailModel != null) {
                    if (orderDetailModel.getPassengeravatar() != null && !orderDetailModel.getPassengeravatar().equals("")) {
                        Uri uri = Uri.parse(orderDetailModel.getPassengeravatar() + RequestUrl.avatar);
                        img_avatar.setImageURI(uri);
                    }
                    if (orderDetailModel.getPassengername() != null && !orderDetailModel.getPassengername().equals("")) {
                        txt_nickname.setText(orderDetailModel.getPassengername());
                    } else {
                        txt_nickname.setVisibility(View.GONE);
                    }
                    tv_phone_number.setText(orderDetailModel.getPassengerphone());
                    tv_money.setText(String.valueOf(orderDetailModel.getPrice()));
                }
            } else {
                re_carinfo.setVisibility(View.VISIBLE);
                if (detailModel != null) {
                    if (detailModel.getAvatar() != null && !detailModel.getAvatar().equals("")) {
                        Uri uri = Uri.parse(detailModel.getAvatar() + RequestUrl.avatar);
                        img_avatar.setImageURI(uri);
                    }
                    if (detailModel.getDrivername() != null && !detailModel.getDrivername().equals("")) {
                        txt_nickname.setText(detailModel.getDrivername());
                    } else {
                        txt_nickname.setVisibility(View.GONE);
                    }
                    tv_phone_number.setText(detailModel.getDriverphone());
                    tv_chepaihao.setText(detailModel.getVehicleno());
                    tv_chexing.setText(detailModel.getCarmodel());
                    tv_color.setText(detailModel.getVehiclecolor());
                    tv_money.setText(String.valueOf(detailModel.getPrice()));
                }
            }

        }

        rb_me_ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                rb_me_ratingBar.setRating(rating);

            }
        });
    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setVisibility(View.GONE);
        tv_call_phone.setTypeface(app.iconfont);
    }

    @Click
    void tv_call_phone() {
        AndPermission.with(this)
                .requestCode(100)
                .permission(Manifest.permission.CALL_PHONE)
                .callback(listener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(context, rationale).
                                show();
                    }
                })
                .start();
    }

    private PermissionListener listener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, List<String> grantedPermissions) {
            // 权限申请成功回调。
            if (requestCode == 100) {
                //打电话
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + ""));
                startActivity(intent);
            }
        }

        @Override
        public void onFailed(int requestCode, List<String> deniedPermissions) {
            // 权限申请失败回调。
            if (requestCode == 100) {
                TastyToast.makeText(context, "获取电话权限失败", TastyToast.LENGTH_SHORT, TastyToast.ERROR);
            }
        }
    };


    @Click
    void back_icon() {
        finish();
    }

    @Click
    void btn_submit() {
        float rb = rb_me_ratingBar.getRating();
        int rate = (int) rb;
        RGradeModel gradeModel = new RGradeModel();
        gradeModel.setId(id);
        gradeModel.setFlag(flag);
        gradeModel.setRate(rate);
        goReate(gradeModel);
    }

    @Background
    void goReate(RGradeModel model) {
        ResultModel<String> res = gradeFinder.grade(model);
        showResult(res);
    }

    @UiThread
    void showResult(ResultModel<String> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                setResult(1);
                finish();
                TastyToast.makeText(this, "评价信息提交成功", app.SHORT, app.SUCCESS);
            } else {
                TastyToast.makeText(this, "评价信息提交失败", app.SHORT, app.ERROR);
            }
        }

    }


}
